package com.mhkj.common.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;



/**
 * Office转换成PDF
 * @author 王楷
 * @date 2018年9月11日 14:25:50
 * @desc 需要OpenOffice第三插件的支持 ,支持window\linux\mac等系统
 */
public class Office2PDF {
	private static final String[] OFFICE_POSTFIXS = { "gif","jpg","jpeg","bmp","png","txt","doc", "docx", "xls", "xlsx", "ppt", "pptx" };

	/**
	 * 根据操作系统的名称，获取OpenOffice的安装目录 如我的安装目录：C:/Program Files/OpenOffice 4
	 */
	private static String getOpenOfficeHome() {
		String osName = System.getProperty("os.name");
		if (Pattern.matches("Linux.*", osName)) {
		return "/opt/libreoffice6.0";
		} else if (Pattern.matches("Windows.*", osName)) {
		return "C:/Program Files (x86)/OpenOffice 4";
		} else if (Pattern.matches("Mac.*", osName)) {
		return "/Application/OpenOffice.org.app/Contents";
		}
		// return null;
		return null;// AitlampUtil.getProjectParam("openOfficeHome");
	}

	/**
	 * 转换文件
	 * @param inputFilePath 转换的office源文件路径
	 * @param outputFilePath 输出目标文件路径
	 */
	private static void converterFile(String inputFilePath, String outputFilePath) {
		File inputFile = new File(inputFilePath);
		File outputFile = new File(outputFilePath);
		// 假如目标路径不存在,则新建该路径
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().mkdirs();
		}
		DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
		// 获取OpenOffice 的安装目录
		String officeHome = getOpenOfficeHome();
		config.setOfficeHome(officeHome);
		config.setPortNumber(8100);
		// 启动OpenOffice的服务
		OfficeManager officeManager = config.buildOfficeManager();
		officeManager.start();
		OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
		converter.convert(inputFile, outputFile);
		System.out.println("文件：" + inputFilePath + "\n转换为\n目标文件：" + outputFile + "\n成功！");
		officeManager.stop();
	}

	/**
	 * 将(.doc|.docx|.xls|.xlsx|.ppt|.pptx)等office文件 转化为pdf文件
	 * @param inputFilePath 待转换的源文件路径
	 * @param outputFilePath 输出的目录文件路径，如果未指定(null)，则按在源文件当前目录生成同名的pdf文件
	 */
	public static boolean openOffice2Pdf(String inputFilePath, String outputFilePath) {
		boolean flag = false;
		File inputFile = new File(inputFilePath);
		ArrayList<String> office_Formats = new ArrayList<String>();
		Collections.addAll(office_Formats, OFFICE_POSTFIXS);
		if ((null != inputFilePath) && (inputFile.exists())) {
			// 判断目标文件路径是否为空
			if (office_Formats.contains(getPostfix(inputFilePath))) {
				if (outputFilePath == null) {
					// 转换后的文件路径
					String inputFilePostfix = getPostfix(inputFilePath);
					String outputFilePath_new = inputFilePath.toLowerCase().replaceAll("." + inputFilePostfix, ".pdf");
					converterFile(inputFilePath, outputFilePath_new);
					flag = true;
				} else {
					converterFile(inputFilePath, outputFilePath);
					flag = true;
				}
			}
		}
		return flag;
	}

	/**
	 * 将(.doc|.docx|.xls|.xlsx|.ppt|.pptx)等office文件 转化为pdf文件
	 * @param inputFilePath 待转换的源文件路径
	 */
	public static String openOffice2Pdf(String inputFilePath) {
		String outputFilePath = null;
		File inputFile = new File(inputFilePath);
		ArrayList<String> office_Formats = new ArrayList<String>();
		Collections.addAll(office_Formats, OFFICE_POSTFIXS);
		if ((null != inputFilePath) && (inputFile.exists())) {
			// 判断目标文件路径是否为空
			if (office_Formats.contains(getPostfix(inputFilePath))) {
				// 转换后的文件路径
				String inputFilePostfix = getPostfix(inputFilePath);
				outputFilePath = inputFilePath.toLowerCase().replaceAll("." + inputFilePostfix, ".pdf");
				converterFile(inputFilePath, outputFilePath);
			}
		}
		return outputFilePath;
	}

	/**
	 * Office转换成PDF
	 * @param officeFile
	 * @return pdfFile
	 */
	public static FileInfo openOffice2Pdf(FileInfo officeFile) {
		FileInfo pdfFile = null;
		String outputFilePath = null;
		File inputFile = officeFile.getFile();
		String inputFilePath = inputFile.getPath();
		ArrayList<String> office_Formats = new ArrayList<String>();
		Collections.addAll(office_Formats, OFFICE_POSTFIXS);
		// 判断文件是否存在
		if ((null != inputFilePath) && (inputFile.exists())) {
			// 判断是否是Office文件
			if (office_Formats.contains(getPostfix(inputFilePath))) {
				// 转换
				String inputFilePostfix = getPostfix(inputFilePath);
				outputFilePath = inputFilePath.toLowerCase().replaceAll("." + inputFilePostfix, ".pdf");
				converterFile(inputFilePath, outputFilePath);
				// 得到pdf文件信息
				pdfFile = new FileInfo(outputFilePath);
				String fileName = officeFile.getFile_name().toLowerCase().replaceAll("." + inputFilePostfix, ".pdf");
				pdfFile.setFile_name(fileName);
			}
		}
		return pdfFile;
	}

	/**
	 * 获取文件的后缀名
	 */
	private static String getPostfix(String inputFilePath) {
		String[] p = inputFilePath.split("\\.");
		if (p.length > 0) {
			return p[p.length - 1];
		} else {
			return null;
		}
	}

	/**
	 * @param args
	 */
	/*public static void main(String[] args) {
		Office2PDF.openOffice2Pdf("F:\\11.jpg", "F:\\121.pdf");
	}*/
}